package org.hefan.basic.service.impl;

import org.hefan.basic.mapper.BaseMapper;
import org.hefan.basic.query.BaseQuery;
import org.hefan.basic.service.IBaseService;
import org.hefan.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * ServiceImpl基类
 * @Service 创建对象,不加service注解因为创建对象不能创建 T 类型对象
 * @param <T> 参数泛型
 */
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    /**
     * 注入baseMapper
     */
    @Autowired
    private BaseMapper<T> baseMapper;

    /**
     * 保存的接口 实现类
     * @param t
     */
    @Override
    @Transactional
    public void save(T t) {
        baseMapper.save(t);
    }

    /**
     * 删除的接口 实现类
     * @param id
     */
    @Override
    @Transactional
    public void remove(Long id) {
        baseMapper.remove(id);
    }

    /**
     * 修改的接口 实现类
     * @param t
     */
    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    /**
     * 通过id加载单个对象
     * @param id
     * @return
     */
    @Override
    public T loadById(Long id) {
        return baseMapper.loadById(id);
    }

    /**
     * 加载全部
     * @return
     */
    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }

    /**
     * 分页查询,高级查询
     * @param baseQuery
     * @return
     */
    @Override
    public PageList<T> queryPage(BaseQuery baseQuery) {
        Long totals = baseMapper.queryCount(baseQuery);
        if (totals == null || totals < 0) {
            return new PageList<>();
        }
        List<T> data = baseMapper.queryData(baseQuery);

        return new PageList<>(totals, data);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void batchRemove(List<Long> ids) {
        baseMapper.batchRemove(ids);
    }

}
